package com.dy.tieba;
import java.net.URLEncoder;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpResponse;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import service.ws.BareResponseProcessor;
import service.ws.CommonWSHandler;
import service.ws.WSHandler;

/**
 * 发帖线程
 */
public class TidPost implements Runnable{
	public UserInfo userInfo;
	TidPost(UserInfo userInfo){
		this.userInfo = userInfo;
	}
	@Override
	public void run() {
		WSHandler wsHandler = new CommonWSHandler("https://tieba.baidu.com/f/commit/thread/add");
		wsHandler.addHeader(new BasicHeader("Cookie", userInfo.cookie));
		TieBaServiceImpl.addHeader(wsHandler);
		//得到用户需要发帖的贴吧
		List<TiebaName> tiebaNamesByUserName = TiebaName.tiebaNamesByUserName(userInfo.userName);
		for (TiebaName tiebaName : tiebaNamesByUserName) {
			String name = "";
			try {
				System.out.println("用户:"+userInfo.userName+" 正在《"+tiebaName.name+"》发帖...");
				name = URLEncoder.encode(tiebaName.name,"UTF-8");
				wsHandler.setMainUrl("https://tieba.baidu.com/f?kw="+name+"&ie=utf-8");
				wsHandler.get();
				HttpResponse httpResponseget = wsHandler.process(BareResponseProcessor.getInstance());
				Document doc = Jsoup.parseBodyFragment(EntityUtils.toString(httpResponseget.getEntity()));
				Elements select = doc.select("script");
				String text = select.get(1).data();
				Pattern pattern = Pattern.compile("'tbs': \"(.*?)\"");
				Pattern pattern2 = Pattern.compile("'id': (.*?)\\,");
				Matcher matcher = pattern.matcher(text);
				Matcher matcher2 = pattern2.matcher(text);
				while (matcher.find()) {
					String tbs = matcher.group(1);
					wsHandler.addParam("tbs", tbs);
				}
				int index = 0;
				while (matcher2.find()) {
					if(index == 1) {
						String fid = matcher2.group(1);
						wsHandler.addParam("fid", fid);
						System.out.println("::"+matcher2.group(1));
					}
					index ++;
				}
				wsHandler.setMainUrl("https://tieba.baidu.com/f/commit/thread/add");
				wsHandler.addHeader(new BasicHeader("Referer","https://tieba.baidu.com/f?kw="+name+"&ie=utf-8"));
				wsHandler.addParam("kw", tiebaName.name);
				wsHandler.addParam("title", tiebaName.name);
				wsHandler.addParam("content", tiebaName.desc);
				wsHandler.addParam("ie", "utf-8");
				wsHandler.addParam("tid", "0");
				wsHandler.addParam("vcode_md5", "");
				wsHandler.addParam("floor_num", "");
				wsHandler.addParam("rich_text", "1");
				wsHandler.addParam("basilisk", "1");
				wsHandler.addParam("prefix", "");
				wsHandler.addParam("mouse_pwd_isclick", "0");
				wsHandler.addParam("__type__", "thread");
				wsHandler.addParam("_BSK", "JVwNUmcLBlEtWHARQ2BUQltFf3U8UBtUSiQISGVHJz41Xz1HGmk/XS5dPCMNW09JKkc7USNwRWVyXBZSZwsGZRA5f31NbhgCSF1lZWoUQgcLfFYKZ0V3cmsSdVhxbHZPAVEuclYfD1oIDW0GRjJafjwbAwF3AhEbJEYLPFB5RglfVHxjaRFQBQxoUGJnGWQgYhJqT1UoKA4QH38kVg8QSXgcawRSclF2fERBFzdEQQdnAQF9W2xHBVpecGNqFUAFEm8QEmcPZCMlXyBDXDkhE1lSMTIWWQYKJkY5XQw9FDcsEQwTMR1UWSwbR3MTKQdFDxQxFjRMGFZKJAhIA0cnPTQcMw5dKiERNF00OgVZQwYnbi1VEzUUNTsPFAY2RW1PKRBwPg0gFFEJDGk0O0sWUlIEA0ogdic8PVIxDFhlJxwFRyglAWhcDCdcLBgMNVQiPw0EJjNUSl82WVQ6FQ8ZXRoSMTI+dgFOUihLSyRBJTgcVTQGUmUpEgNWCThIQEUfLGomGAw1Sy4kGzUMaUNBWCwPVh0YYBFVHjQgOz9GAV5RI0tALFsifDZVJCJSPScVEFceBDd/XwUsW30YXDEMZWReBwIpQkEHZwYAfVtsAkIfAml1KhdXDRwaDkh2B2R8c0VhTQlrCRIPWjE7BQIfR3kId2MXPlwoKQ1BLREREgV0ThMILhtABENHBCcqSRBgWy8sTzEac2NmHmNZE2EPNSF+EXtEQUMCLAgYUR07V25+PQkRKlxBBHBCHW9Pfk8IXUl0ZGkFJlZYLBVPagB1Z38DZk0faydMVwl9IxZYT0VrSW4WRHAJc2pOTUE2AAYRZUwDb01uAgFIXWcxL0sWQ1ciCQYxWhUkI1k+CBtgZAZVaDM2EERcDGlLMFAbDRg6fFJDDnQTHgknFEA2DSUFWzUGCSFqTxIVEm8CF2cPZmJhAWdfBnh1UVdEb3VeD2Q8BWR9GFw8CmVkXhURMFQICSRGEWVBKhdcGQJpdTQUVw0ef1cXcgVzYWAccgsCa35fO2YRG0YBCAh7CmUURmcIa3wQUkF/ERYbdEIDalB9WhIdVGdtekMUW00oGg==");
				wsHandler.post();
				HttpResponse httpResponse = wsHandler.process(BareResponseProcessor.getInstance());
			    String responseString = EntityUtils.toString(httpResponse.getEntity());
			    JSONObject result = new JSONObject(responseString);
			    int no = result.getInt("no");
			    if(no==0) {
			    	System.out.println("用户:"+userInfo.userName+" 在《"+tiebaName.name+"》发帖成功");
			    	JSONObject data = result.getJSONObject("data");
			    	String tidUrl = "http://tieba.baidu.com/p/"+data.getInt("tid");
			    	System.out.println("帖子URl："+tidUrl);
			    }else {
			    	
			    }
			    //发帖间隔10分钟
				Thread.sleep(60 * 1000 * 10);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
	}
	
}
